<?

class UploadController extends Main_Controller_Action {

    public function preDispatch() {
		/*if (!$this->loggedUser) {
		    die('You must be logged to upload files');
		} else {
		    
		    $commonLocale = Translator::loadTranslation($this->currentLang, 'common');
		    $this->view->commonLocale = $commonLocale;
		}
*/
    }

    function indexAction() {

		
	$this->_helper->viewRenderer->setNoRender();
	$auth = Zend_Auth::getInstance();
	if ($auth->hasIdentity()) {
	    $userDetails = $auth->getIdentity();
	    $this->view->userDetails = $userDetails;
	}

	$registry = Zend_Registry::getInstance();
	$config = $registry->get('config');

	require_once(dirname(__FILE__) . '/../models/Files.php');

	if (!empty($_FILES)) {
		
		//Database::instance()->getConn()->debug=1;
	    $exFileName = explode('.', $_FILES['Filedata']['name']);

	    $file = new Files();
	    $file->setFileTypesId(intval($this->getRequest()->getParam('fileTypesId')));
	    $file->setObjectId(NULL);
	    $file->setName($_FILES['Filedata']['name']);
	    $file->setSize($_FILES['Filedata']['size']);
	    $file->setType($_FILES['Filedata']['type']);
	    $file->setExtension($exFileName[1]);
	    $file->setUsersId(!is_null(($this->loggedUser)?$this->loggedUser->getId():NULL));
		
	    ObjectWatcher::flushOperations();


	    if ($file->getId() != null) {

		$tempFile = $_FILES['Filedata']['tmp_name'];
		$targetPath = $_SERVER['DOCUMENT_ROOT'] . $this->getRequest()->getParam('destination') . '/';
		$targetFile = str_replace('//', '/', $targetPath) . $file->getId() . '.' . $file->getExtension();

		if (!move_uploaded_file($tempFile, $targetFile)) {
		    echo 'error - unable to save file';
		} else {

		    if (in_array($exFileName[1], array('JPEG', 'jpeg', 'JPG', 'jpg', 'png', 'PNG', 'gif', 'GIF'))) {
				$file->createThumb($this->getRequest()->getParam('destination'), $config->thumbnails->standard->width, $config->thumbnails->standard->height, 'zc', false);
				$file->createThumb($this->getRequest()->getParam('destination'), $config->thumbnails->medium->width, $config->thumbnails->medium->height, 'zc', false);
				$file->createThumb($this->getRequest()->getParam('destination'), $config->thumbnails->cart->width, $config->thumbnails->cart->height, 'zc', false);
				$file->createThumb($this->getRequest()->getParam('destination'), $config->thumbnails->favourites->width, $config->thumbnails->favourites->height, 'zc', false);
				$file->createThumb($this->getRequest()->getParam('destination'), $config->thumbnails->comments->width, $config->thumbnails->comments->height, 'zc', false);
				$file->createThumb($this->getRequest()->getParam('destination'), $config->thumbnails->recently_view->width, $config->thumbnails->recently_view->height, 'zc', false);
		    }
		    echo json_encode(array(
			'filesId' => $file->getId(),
			'path' => $this->getRequest()->getParam('destination') . '/',
			'name' => $file->getId() . '.' . $file->getExtension(),
			'ext' => $file->getExtension()
			    )
		    );
		}
	    } else {
		echo "File upload failed!";
	    }
	} else {
	    echo "File upload failed!";
	}


	exit();
    }
}
?>